/**
 * Filename : functions2.c
 * Author   : Zhangjinze (799423351@qq.com)
 * Data     : 20131224
 */

#include "caipiao.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/**
 * GenerateAllGroup(): Generate All possible groups.
 */
void showProcessBar(int i, int j)
{
    if(i == (j/10)) {
        printf("[==       01         ]\r");
    } else if(i == (j/10)*2) {
        printf("[====     02         ]\r");
    } else if(i == (j/10)*3) {
        printf("[======   03         ]\r");
    } else if(i == (j/10)*4) {
        printf("[======== 04         ]\r");
    } else if(i == (j/10)*5) {
        printf("[=========05         ]\r");
    } else if(i == (j/10)*6) {
        printf("[=========06=        ]\r");
    } else if(i == (j/10)*7) {
        printf("[=========07===      ]\r");
    } else if(i == (j/10)*8) {
        printf("[=========08=====    ]\r");
    } else if(i == (j/10)*9) {
        printf("[=========09=======  ]\r");
    } else if(i == (j/10)*10) {
        printf("[=========10=========]\n");
    }
}

/**
 * get Maxinum ReNumber
 */
int getMaxinumReNumber(GroupList *group, int stage)
{
    int i, j, MaxinumRet, MaxinumTemp;
    struct OneStageNumbers ArrNum;
    GroupList *compareList;
    FILE *fileP;
    char c;

    MaxinumRet = 0;
    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }
    compareList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<7; i++) {
            compareList->Ball[i] = ArrNum.Num[i+1];
        }

        MaxinumTemp = 0;
        if((ArrNum.Num[0] != stage) || (stage == 0)) {
            for(i=0; i<6; i++) {
                for(j=0; j<6; j++) {
                    if(group->Ball[i] == compareList->Ball[j]) {
                        //printf("group ball:%d, compare ball:%d\n", group->Ball[i], compareList->Ball[j]);
                        MaxinumTemp++;
                        break;
                    }
                }
            }
            if (MaxinumTemp > MaxinumRet) {
                MaxinumRet = MaxinumTemp;
            }
        }
    }

    fclose(fileP);
    return MaxinumRet;
}

/**
 * get Maxinum ReNumber
 */
void showMaxinumReNumber()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, maxinumReNumberRet, arrMaxinumReNumber[7];
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<7; i++)
        arrMaxinumReNumber[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<6; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        maxinumReNumberRet = getMaxinumReNumber(numList, ArrNum.Num[0]);
        arrMaxinumReNumber[maxinumReNumberRet]++;
    }

    printf("Maxinum Re-Number table is :\n");
    for(i=0; i<7; i++) {
        printf("Re-Number%d: ", i);
        for(j=0; j<arrMaxinumReNumber[i]/3; j++) {
            printf("|");
        }
        printf("%d\n", arrMaxinumReNumber[i]);
    }
    fclose(fileP);
}

/**
 * int getReNo(); get re No. from stages before
 */
int getBigToSmall(GroupList *group)
{
    int i, tempBig, tempSmall, ret;

    tempBig = 0;
    tempSmall = 0;
    for(i=0; i<6; i++) {
        if(group->Ball[i] >= 17) {
            // big number
            tempBig++;
        } else if (group->Ball[i] < 17) {
            // small number
            tempSmall++;
        }
    }

    // ret: 0(3:3) 1(4:2) 2(2:4) 3(5:1) 4(1:5) 5(6:0) 6(0:6)
    if ((tempBig == 3) && (tempSmall == 3)) {
        //3:3
        return 0;
    } else if ((tempBig == 4) && (tempSmall == 2)) {
        //4:2
        return 1;
    } else if ((tempBig == 2) && (tempSmall == 4)) {
        //2:4
        return 2;
    } else if ((tempBig == 5) && (tempSmall == 1)) {
        //5:1
        return 3;
    } else if ((tempBig == 1) && (tempSmall == 5)) {
        //1:5
        return 4;
    } else if ((tempBig == 6) && (tempSmall == 0)) {
        //6:0
        return 5;
    } else if ((tempBig == 0) && (tempSmall == 6)) {
        //0:6
        return 6;
    }
}

/**
 * void showBigToSmall()
 */
void showBigToSmall()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, BigtoSmallRet, arrBigtoSmall[7];
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<7; i++)
        arrBigtoSmall[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        BigtoSmallRet = getBigToSmall(numList);
        arrBigtoSmall[BigtoSmallRet]++;
    }

    printf("Big to small table is :\n");
    for(i=0; i<7; i++) {
        if(i == 0)
            printf("33: ");
        else if (i == 1)
            printf("42: ");
        else if (i == 2)
            printf("24: ");
        else if (i == 3)
            printf("51: ");
        else if (i == 4)
            printf("15: ");
        else if (i == 5)
            printf("60: ");
        else if (i == 6)
            printf("06: ");

        for(j=0; j<arrBigtoSmall[i]/2; j++) {
            printf("|");
        }
        printf("%d\n", arrBigtoSmall[i]);
    }

    fclose(fileP);
}

/**
 * int getReNo(); get re No. from stages before
 */
int getReNo(GroupList numListPre, GroupList numList)
{
    int i, j, ret;

    ret = 0;
    for(i=0; i<6; i++) {
        for(j=0; j<6; j++) {
            if(numList.Ball[i] == numListPre.Ball[j]) {
                // get equal number
                ret++;
            }
        }
    }

    return ret;
}

/**
 * showReNo();
 */
void showReNo()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList numList, numListPre;
    int i, j, arrReNo[6], reNoRet;
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<6; i++)
        arrReNo[i] = 0;
    //numListPre = (GroupList *)malloc(sizeof(GroupList));
    //numList = (GroupList *)malloc(sizeof(GroupList));
    if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
        exit(1);
    }
    for(i=0; i<7; i++) {
        numListPre.Ball[i] = ArrNum.Num[i+1];
    }
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList.Ball[i] = ArrNum.Num[i+1];
        }
        reNoRet = getReNo(numListPre, numList);
        arrReNo[reNoRet]++;
        numListPre = numList;
    }

    printf("Re no. table is :\n");
    for(i=0; i<6; i++) {
        printf("Re No %d: ", i);
        for(j=0; j<arrReNo[i]/2; j++) {
            printf("|");
        }
        printf("%d\n", arrReNo[i]);
    }
    fclose(fileP);
}

/**
 * int getConsecutiveNumber(): get one gourp's consecutive number
 */
int getConsecutiveNumber(GroupList *group)
{
    int i, ret;

    ret = 0;
    for(i=0; i<5; i++) {
        if ((group->Ball[i]+1) == group->Ball[i+1]) {
            ret++;
        }
    }

    return ret;
}

/**
 * void showConsecutiveNumber(): show all gourp's consecutive number
 */
void showConsecutiveNumber()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, consecutiveNumberRet, arrConsecutiveNumber[6];
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<6; i++)
        arrConsecutiveNumber[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        consecutiveNumberRet = getConsecutiveNumber(numList);
        arrConsecutiveNumber[consecutiveNumberRet]++;
    }

    printf("Consecutive Number of the stages before table is:\n");
    for(i=0; i<6; i++) {
        printf("%d: ", i);
        for(j=0; j<arrConsecutiveNumber[i]/2; j++)
            printf("|");
        printf("%d\n", arrConsecutiveNumber[i]);
    }
    fclose(fileP);
}

/**
 * int getSum(): get one gourp's sum value
 */
int getSum(GroupList *group)
{
    int sum, i;

    sum = 0;
    for(i=0; i<6; i++) {
        sum = sum + group->Ball[i];
    }

    // 0:  1- 10,  1: 11- 20,  2: 21- 30,  3: 31- 40,  4: 41- 50,  5: 51- 60,  6: 61- 70,  7: 71- 80
    // 8: 81- 90,  9: 91-100, 10:101-110, 11:111-120, 12:121-130, 13:131-140, 14:141-150, 15:151-160
    //16:161-170, 17:171-180, 18:181-190, 19:191-200
    if (sum <= 10) {
        return 0;
    } else if (sum <= 20) {
        return 1;
    } else if (sum <= 30) {
        return 2;
    } else if (sum <= 40) {
        return 3;
    } else if (sum <= 50) {
        return 4;
    } else if (sum <= 60) {
        return 5;
    } else if (sum <= 70) {
        return 6;
    } else if (sum <= 80) {
        return 7;
    } else if (sum <= 90) {
        return 8;
    } else if (sum <= 100) {
        return 9;
    } else if (sum <= 110) {
        return 10;
    } else if (sum <= 120) {
        return 11;
    } else if (sum <= 130) {
        return 12;
    } else if (sum <= 140) {
        return 13;
    } else if (sum <= 150) {
        return 14;
    } else if (sum <= 160) {
        return 15;
    } else if (sum <= 170) {
        return 16;
    } else if (sum <= 180) {
        return 17;
    } else if (sum <= 190) {
        return 18;
    } else if (sum <= 200) {
        return 19;
    }
}

/**
 * void showSum(): Show all group's sum
 */
void showSum()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, sumRet, arrSum[20];
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<20; i++)
        arrSum[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        sumRet = getSum(numList);
        if (sumRet == 0) {
            arrSum[0]++;
        } else if (sumRet == 1) {
            arrSum[1]++;
        } else if (sumRet == 2) {
            arrSum[2]++;
        } else if (sumRet == 3) {
            arrSum[3]++;
        } else if (sumRet == 4) {
            arrSum[4]++;
        } else if (sumRet == 5) {
            arrSum[5]++;
        } else if (sumRet == 6) {
            arrSum[6]++;
        } else if (sumRet == 7) {
            arrSum[7]++;
        } else if (sumRet == 8) {
            arrSum[8]++;
        } else if (sumRet == 9) {
            arrSum[9]++;
        } else if (sumRet == 10) {
            arrSum[10]++;
        } else if (sumRet == 11) {
            arrSum[11]++;
        } else if (sumRet == 12) {
            arrSum[12]++;
        } else if (sumRet == 13) {
            arrSum[13]++;
        } else if (sumRet == 14) {
            arrSum[14]++;
        } else if (sumRet == 15) {
            arrSum[15]++;
        } else if (sumRet == 16) {
            arrSum[16]++;
        } else if (sumRet == 17) {
            arrSum[17]++;
        } else if (sumRet == 18) {
            arrSum[18]++;
        } else if (sumRet == 19) {
            arrSum[19]++;
        }
    }

    printf("Sums table are:\n");
    for(i=0; i<20; i++) {
        if (i == 0) {
            printf(" 0:  1- 10: ");
        } else if (i == 1) {
            printf(" 1: 11- 20: ");
        } else if (i == 2) {
            printf(" 2: 21- 30: ");
        } else if (i == 3) {
            printf(" 3: 31- 40: ");
        } else if (i == 4) {
            printf(" 4: 41- 50: ");
        } else if (i == 5) {
            printf(" 5: 51- 60: ");
        } else if (i == 6) {
            printf(" 6: 61- 70: ");
        } else if (i == 7) {
            printf(" 7: 71- 80: ");
        } else if (i == 8) {
            printf(" 8: 81- 90: ");
        } else if (i == 9) {
            printf(" 9: 91-100: ");
        } else if (i == 10) {
            printf("10:101-110: ");
        } else if (i == 11) {
            printf("11:111-120: ");
        } else if (i == 12) {
            printf("12:121-130: ");
        } else if (i == 13) {
            printf("13:131-140: ");
        } else if (i == 14) {
            printf("14:141-150: ");
        } else if (i == 15) {
            printf("15:151-160: ");
        } else if (i == 16) {
            printf("16:161-170: ");
        } else if (i == 17) {
            printf("17:171-180: ");
        } else if (i == 18) {
            printf("18:181-190: ");
        } else if (i == 19) {
            printf("19:191-200: ");
        }
        for(j=0; j<arrSum[i]/2; j++)
            printf("|");
        printf("%d\n", arrSum[i]);
    }
    fclose(fileP);
}



/**
 * int getSameNumberOfMantissa(GroupList *group): get one group's same number of mantissa
 */
int getSameNumberOfMantissa(GroupList *group)
{
    int i, j, tempi[5], tempj;

    tempj = 1;
    for(i=0; i<5; i++) {
        tempi[i] = 1;
        for(j=i+1; j<6; j++) {
            if ((group->Ball[i]%10) == (group->Ball[j]%10)) {
                tempi[i]++;
            }
        }
        if (tempi[i] > tempj) {
            tempj = tempi[i];
        }
    }

    return tempj;
}

/**
 * void showSameNumberOfMantissa(): show same number of mantissa
 */
void showSameNumberOfMantissa()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, MantissaSameNumberRet, arrMantissaSameNumber[5];
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<5; i++)
        arrMantissaSameNumber[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            //printf("Read stage:%d error!\n", stageN);
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        // 0:1, 1:2, 2:3, 3:4, 4:5
        MantissaSameNumberRet = getSameNumberOfMantissa(numList);
        arrMantissaSameNumber[MantissaSameNumberRet-1]++;
    }

    printf("Biggest mantissa same numbers are:\n");
    for(i=0; i<5; i++) {
        printf("%d: ", i+1);
        for(j=0; j<arrMantissaSameNumber[i]/3; j++)
            printf("|");
        printf("%d\n", arrMantissaSameNumber[i]);
    }
    fclose(fileP);
}


/**
 * int getACValue(): get one group's AC value
 */
int getParityRatio(GroupList *group)
{
    int i, tempi,tempj, arr[7];

    tempi = 0; // odd number
    tempj = 0; // even number

    for(i=0; i<6; i++) {
        if ((group->Ball[i]%2) == 0) {
            tempj++;
        } else if ((group->Ball[i]%2) == 1) {
            tempi++;
        }
    }

    if ((tempi == 3) && (tempj == 3)) {
        return THREETOTHREE;
    } else if ((tempi == 4) && (tempj == 2)) {
        return FOURTOTWO;
    } else if ((tempi == 2) && (tempj == 4)) {
        return TWOTOFOUR;
    } else if ((tempi == 5) && (tempj == 1)) {
        return FIVETOONE;
    } else if ((tempi == 1) && (tempj == 5)) {
        return ONETOFIVE;
    } else if ((tempi == 6) && (tempj == 0)) {
        return SIXTOO;
    } else if ((tempi == 0) && (tempj == 6)) {
        return OTOSIX;
    }
}

/**
 * void showParityRatio(): show stages' AC value before
 */
void showParityRatio()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, ParityRatioRet, arrParityRatio[7]; // AC value without blue ball is between 0-10
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<7; i++)
        arrParityRatio[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            //printf("Read stage:%d error!\n", stageN);
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        // 0:(3:3); 1:(4:2); 2:(2:4); 3:(5:1); 4:(1:5); 5:(6:0); 6:(0:6)
        ParityRatioRet = getParityRatio(numList);
        arrParityRatio[ParityRatioRet]++;
    }

    printf("Parity ratio table is as follows:\n");
    for(i=0; i<7; i++) {
        if (i == 0)
            printf("33: ");
        else if (i == 1)
            printf("42: ");
        else if (i == 2)
            printf("24: ");
        else if (i == 3)
            printf("51: ");
        else if (i == 4)
            printf("15: ");
        else if (i == 5)
            printf("60: ");
        else if (i == 6)
            printf("06: ");
        for(j=0; j<arrParityRatio[i]; j++)
            printf("|");
        printf("%d\n", arrParityRatio[i]);
    }
    fclose(fileP);
}

/**
 * int getACValue(): get one group's AC value
 */
int getACValue(GroupList *group)
{
    int i, j, a, tempi,tempj, arr[15], ret;

    a = 0;
    tempi = 0;
    // calculate difference between every two numbers.
    for(i=0; i<5; i++) {
        for(j=i+1; j<6; j++) {
            tempi = abs(group->Ball[i]-group->Ball[j]);
            arr[a] = tempi;
            a++;
        }
    }

    // calculate the number of mutually different numbers (different with others)
    tempj = 15;
    for(i=0; i<14; i++) {
        for(j=i+1; j<15; j++) {
            if (arr[i] == arr[j]) {
                tempj--;
            }
        }
    }

    // number - 5  = AC number.
    ret = tempj - 5;

    return ret;
}

/**
 * void showACValue(): show stages' AC value before
 */
void showACValue()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int i, j, ACValueRet, arrACValue[11]; // AC value without blue ball is between 0-10
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<11; i++)
        arrACValue[i] = 0;

    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            //printf("Read stage:%d error!\n", stageN);
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        ACValueRet = getACValue(numList);
        arrACValue[ACValueRet]++;
    }

    printf("AC value table is as follows:\n");
    for(i=0; i<11; i++) {
        printf("%02d: ", i);
        for(j=0; j<arrACValue[i]; j++)
            printf("|");
        printf("%d\n", arrACValue[i]);
    }
    fclose(fileP);
}

/**
 * int getInterval(): Get one group's numbers' range
 */
int getInterval(GroupList *group)
{
    int i, intervalI, tempi;

    intervalI = group->Ball[1] - group->Ball[0];
    tempi = 0;
    for(i=1; i<5; i++) {
        tempi = abs(group->Ball[i+1] - group->Ball[i]);
        if (tempi > intervalI) {
            intervalI = tempi;
        }
    }

    return intervalI;
}

/**
 * showIntervalStagesbefore(): show the interval of stages before.
 */
void showIntervalStagesbefore()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    GroupList *numList;
    int arrInterval[28], intervalRet, i, j;
    char c;

    if((fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL) {
        exit(1);
    }

    for(i=0; i<28; i++)
        arrInterval[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            //printf("Read stage:%d error!\n", stageN);
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        intervalRet = getInterval(numList);
        arrInterval[intervalRet-1]++;
    }
    for(i=0; i<28; i++) {
        printf("%02d:", i+1);
        for(j=0; j<arrInterval[i]; j++)
            printf("|");
        printf("%d\n", arrInterval[i]);
    }
    fclose(fileP);
}

/**
 * int getRange(): Get one group's numbers' range
 */
int getRange(GroupList *group)
{
    int i, ret;
    int range1, range2, range3;

    range1 = 0;
    range2 = 0;
    range3 = 0;
    for(i=0; i<6; i++) {
        //printf("getRange: %d", group->Ball[i]);
        if ( (group->Ball[i] > 0) && (group->Ball[i] < 12) )
            range1++;
        if ( (group->Ball[i] > 11) && (group->Ball[i] < 23) )
            range2++;
        if ( (group->Ball[i] > 22) && (group->Ball[i] < 34) )
            range3++;
    }
    //printf("---->%d%d%d\n", range1, range2, range3);

    if((range1==2) && (range2==2) && (range3==2)){
        // 222
        return TWOTWOTWO;
    } else if ((range1==1) && (range2==2) && (range3==3)) {
        // 123
        return ONETWOTHREE;
    } else if ((range1==1) && (range2==3) && (range3==2)) {
        // 132
        return ONETHREETWO;
    } else if ((range1==3) && (range2==1) && (range3==2)) {
        // 312
        return THREEONETWO;
    } else if ((range1==3) && (range2==2) && (range3==1)) {
        // 321
        return THREETWOONE;
    } else if ((range1==2) && (range2==1) && (range3==3)) {
        // 213
        return TWOONETHREE;
    } else if ((range1==2) && (range2==3) && (range3==1)) {
        // 231
        return TWOTHREEONE;
    } else if ((range1==4) && (range2==1) && (range3==1)) {
        // 411
        return FOURONEONE;
    } else if ((range1==1) && (range2==4) && (range3==1)) {
        // 141
        return ONEFOURONE;
    } else if ((range1==1) && (range2==1) && (range3==4)) {
        // 114
        return ONEONEFOUR;
    } else if ((range1==3) && (range2==3) && (range3==0)) {
        // 330
        return THREETHREEO;
    } else if ((range1==3) && (range2==0) && (range3==3)) {
        // 303
        return THREEOTHREE;
    } else if ((range1==0) && (range2==3) && (range3==3)) {
        // 033
        return OTHREETHREE;
    } else if ((range1==0) && (range2==4) && (range3==2)) {
        // 042
        return OFOURTWO;
    } else if ((range1==2) && (range2==4) && (range3==0)) {
        // 240
        return TWOFOURO;
    } else if ((range1==4) && (range2==2) && (range3==0)) {
        // 420
        return FOURTWOO;
    } else if ((range1==4) && (range2==0) && (range3==2)) {
        // 402
        return FOUROTWO;
    } else if ((range1==2) && (range2==0) && (range3==4)) {
        // 204
        return TWOOFOUR;
    } else if ((range1==0) && (range2==2) && (range3==4)) {
        // 024
        return OTWOFOUR;
    } else if ((range1==5) && (range2==0) && (range3==1)) {
        // 501
        return FIVEOONE;
    } else if ((range1==1) && (range2==0) && (range3==5)) {
        // 105
        return ONEOFIVE;
    } else if ((range1==1) && (range2==5) && (range3==0)) {
        // 150
        return ONEFIVEO;
    } else if ((range1==0) && (range2==1) && (range3==5)) {
        // 015
        return OONEFIVE;
    } else if ((range1==5) && (range2==1) && (range3==0)) {
        // 510
        return FIVEONEO;
    } else if ((range1==0) && (range2==5) && (range3==1)) {
        // 051
        return OFIVEONE;
    } else if ((range1==0) && (range2==0) && (range3==6)) {
        // 006
        return OOSIX;
    } else if ((range1==6) && (range2==0) && (range3==0)) {
        // 600
        return SIXOO;
    } else if ((range1==0) && (range2==6) && (range3==0)) {
        // 060
        return OSIXO;
    }
}


/**
 * show showAllStageRange(): show all stages' range
 */
void showAllStageRange()
{
    FILE *fileP;
    struct OneStageNumbers ArrNum;
    int ranges[28], stageN, rangeRet, j, i=0;
    GroupList *numList;
    char c;

    if ( (fileP = OpenAFile("LuckyNumbers.txt", "a+")) == NULL ) {
        exit(1);
    }

    for(i=0; i<28; i++)
        ranges[i] = 0;
    numList = (GroupList *)malloc(sizeof(GroupList));
    //printf("Now trvasal the file.\n");
    while(!feof(fileP)) {
        if (!fscanf(fileP, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d\n", &ArrNum.Num[0], &c, &ArrNum.Num[1], &c, &ArrNum.Num[2], &c, &ArrNum.Num[3], &c,
                                                                        &ArrNum.Num[4], &c, &ArrNum.Num[5], &c, &ArrNum.Num[6], &c, &ArrNum.Num[7])) {
            //printf("Read stage:%d error!\n", stageN);
            exit(1);
        }
        for(i=0; i<7; i++) {
            numList->Ball[i] = ArrNum.Num[i+1];
        }
        rangeRet = getRange(numList);
        ranges[rangeRet]++;
        //printf("ranges[%d]:%d\n", rangeRet, ranges[rangeRet]);
    }
    for(i=0; i<28; i++) {
        if (i == 0) {
            printf("222: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 1) {
            printf("123: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 2) {
            printf("132: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 3) {
            printf("312: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 4) {
            printf("321: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 5) {
            printf("213: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 6) {
            printf("231: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 7) {
            printf("411: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 8) {
            printf("141: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 9) {
            printf("114: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 10) {
            printf("330: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 11) {
            printf("303: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 12) {
            printf("033: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 13) {
            printf("042: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 14) {
            printf("240: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 15) {
            printf("420: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 16) {
            printf("402: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 17) {
            printf("204: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 18) {
            printf("024: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 19) {
            printf("501: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 20) {
            printf("105: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 21) {
            printf("150: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 22) {
            printf("015: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 23) {
            printf("510: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 24) {
            printf("051: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 25) {
            printf("006: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 26) {
            printf("600: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        } else if (i == 27) {
            printf("060: ");
            for(j=0; j<ranges[i]; j++)
                printf("|");
            printf("%d\n", ranges[i]);
        }
    }
    fclose(fileP);
}

